﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using DotNetUI.Models;

namespace DotNetUI.Controls
{
    public class DotNetWindow : Window
    {
        static DotNetWindow()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(DotNetWindow), new FrameworkPropertyMetadata(typeof(DotNetWindow)));
        }
        private User32.POINT _doubleClickPoint;
        Button _minButton;
        Button _maxButton;
        Button _closeButton;
        public override void OnApplyTemplate()
        {
            _minButton = (Button)this.Template.FindName("_minButton", this);
            _maxButton = (Button)this.Template.FindName("_maxButton", this);
            _closeButton = (Button)this.Template.FindName("_closeButton", this);
            var titlePanel=(Grid) this.Template.FindName("_grid", this);
            titlePanel.MouseDown += TitlePanel_MouseDown;
            titlePanel.PreviewMouseLeftButtonDown += TitlePanel_PreviewMouseLeftButtonDown;
            titlePanel.MouseMove += TitlePanel_MouseMove;
            titlePanel.MouseLeftButtonDown += TitlePanel_MouseLeftButtonDown;
            _minButton.Click += _minButton_Click;
            _maxButton.Click += _maxButton_Click;
            _closeButton.Click += _closeButton_Click;
            base.OnApplyTemplate();
        }

        private void TitlePanel_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (e.ClickCount != 2) return;

            User32.GetCursorPos(out _doubleClickPoint);
            if (WindowState == WindowState.Normal)
            {
                WindowState = WindowState.Maximized;
            }
            else
            {
                WindowState = WindowState.Normal;
            }
        }

        private void TitlePanel_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.LeftButton!=MouseButtonState.Pressed)
            {
                return;
            }
            User32.GetCursorPos(out var pos);
            if (pos.X == _doubleClickPoint.X && pos.Y == _doubleClickPoint.Y)
            {
                return;
            }
            if (this.WindowState==WindowState.Maximized)
            {
                var screenPoint = PointToScreen(e.MouseDevice.GetPosition(this));
                screenPoint.X /= Dpi.SystemDpiXScale();
                screenPoint.Y /=Dpi.SystemDpiYScale();

                // TODO: refine the Left value to be more accurate
                // - This calculation is good enough using the center
                //   of the titlebar, however this isn't quite accurate for
                //   how the OS operates.
                // - It should be set as a % (e.g. screen X / maximized width),
                //   then offset from the left to line up more naturally.
                this.Left = screenPoint.X - (this.RestoreBounds.Width * 0.5);
                this.Top = screenPoint.Y;

                // style has to be quickly swapped to avoid restore animation delay
                var style = this.WindowStyle;
                this.WindowStyle = WindowStyle.None;
                this.WindowState = WindowState.Normal;
                this.WindowStyle = style;
            }
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                this.DragMove();
            }
        }

        private void TitlePanel_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            //if (e.ClickCount >= 2)
            //{
            //    _maxButton_Click(sender, null);
            //}
        }

        private void TitlePanel_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (e.LeftButton==MouseButtonState.Pressed)
            {
                this.DragMove();
            }
        }

        private void _closeButton_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void _maxButton_Click(object sender, RoutedEventArgs e)
        {
            if (this.WindowState==WindowState.Normal)
            {
                this.WindowState = WindowState.Maximized;

            }
            else
            {
                this.WindowState = WindowState.Normal;

            }
        }

        private void _minButton_Click(object sender, RoutedEventArgs e)
        {
            this.WindowState = WindowState.Minimized;
        }
    }
}
